//
//  AppMacro.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2018/8/8.
//  Copyright © 2018年 杨东宝. All rights reserved.
//



// MARK: - 系统相关 =================================================
public let kApplication = UIApplication.shared
public let kAppWindow = { () -> UIWindow? in
    var window:UIWindow? = nil
    if #available(iOS 13.0, *) {
    
        return UIApplication.shared.windows[0]
    }else{
        return  UIApplication.shared.keyWindow
    }
}

public let kAppBundleInfoVersion = Bundle.main.infoDictionary ?? Dictionary()
public let kAppBundleVersion = (kAppBundleInfoVersion["CFBundleShortVersionString" as String] as? String ) ?? ""
public let kAppBundleBuild = (kAppBundleInfoVersion["CFBundleVersion"] as? String ) ?? ""
public let kAppDisplayName = (kAppBundleInfoVersion["CFBundleDisplayName"] as? String ) ?? ""

public let kiOSBase = 8.0
public let kOSGreaterOrEqualToiOS8 = ( (Double(UIDevice.current.systemVersion) ?? kiOSBase) > 8.0 ) ? true : false;
public let kOSGreaterOrEqualToiOS9 = ((Double(UIDevice.current.systemVersion) ?? kiOSBase) >= 9.0 ) ? true : false
public let kOSGreaterOrEqualToiOS10 = ((Double(UIDevice.current.systemVersion) ?? kiOSBase) >= 10.0 ) ? true : false
public let kOSGreaterOrEqualToiOS11 = ((Double(UIDevice.current.systemVersion) ?? kiOSBase) >= 11.0 ) ? true : false

// 设备宽高、机型
public let kScreenHeight    = UIScreen.main.bounds.size.height
public let kScreenWidth     = UIScreen.main.bounds.size.width
public let kScreen_Bounds   = UIScreen.main.bounds
public let kStatusBarheight = { () -> CGFloat in
    var statusBarHeight:CGFloat = 0.0;
    if  #available(iOS 13.0, *)  {
        let statusbarManger = UIApplication.shared.windows.first?.windowScene?.statusBarManager
        statusBarHeight = statusbarManger?.statusBarFrame.size.height ?? 20
    }else{
        statusBarHeight = UIApplication.shared.statusBarFrame.size.height
    }
    return statusBarHeight
}
public let kNavBarHeight: CGFloat = 44
public let kTopHeight:CGFloat =  kStatusBarheight() + kNavBarHeight
public let kTabBarHeight: CGFloat =  kStatusBarheight() > 20 ? 83:49
public let kTabbarSafeBottomMargin: CGFloat =  kStatusBarheight() > 20 ? 20:0


// 判断iPhone4
public let kIPHONE4_DEV:Bool! = (UIScreen.main.bounds.size.height == 480) ? true : false
// 判断iPhone5/5c/5s
public let kIPHONE5_DEV:Bool! = (UIScreen.main.bounds.size.height == 568) ? true : false
// 判断iPhone6/6s
public let kIPHONE6s_DEV:Bool! = (UIScreen.main.bounds.size.height == 667) ? true : false
// 判断iPhone6p/7p/8p
public let kIPHONE6p_DEV:Bool! = (UIScreen.main.bounds.size.height == 736) ? true : false
// 判断iPhoneX
public let kIPHONEX_DEV:Bool! = (UIScreen.main.bounds.size.height == 812) ? true : false
// 判断iPhoneXR
public let kIPHONEXR_DEV:Bool! = (UIScreen.main.bounds.size.height == 896) ? true : false
// 判断iPhoneXSMAX
public let kIPHONEXPRO_DEV:Bool! = (UIScreen.main.bounds.size.height == 896) ? true : false

// 判断iPad
public let kIPAD_DEV:Bool! = (UIDevice.current.userInterfaceIdiom == .pad) ? true : false

public var isSimulator: Bool {
    #if targetEnvironment(simulator)
    return true
    #else
    return false
    #endif
}
//MARK:- DEBUG打印函数 =================================================
func DebugLog<T>(_ msg:T ,
                 _ file:NSString = #file,
                 _ line:Int = #line,
                 _ fn:String = #function){
    #if DEBUG
    let prefix = "\(file.lastPathComponent)---\(line)---\(fn):"
    print(prefix,msg)
    #endif
}
// MARK: - 图片、颜色 =================================================

// 根据imageName创建一个UIImage
public let imageNamed:((String) -> UIImage? ) = { (imageName : String) -> UIImage? in
    return UIImage.init(named: imageName)
}

// 通过 十六进制与alpha来设置颜色值（ 样式：0xff00ff ）不带黑暗适配
public let HexRGBAlpha:((Int,Float) -> UIColor) = { (rgbValue : Int, alpha : Float ) -> UIColor in
    return UIColor(red: CGFloat(CGFloat((rgbValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((rgbValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(rgbValue & 0xFF)/255), alpha: CGFloat(alpha))
}
// 通过 十六进制设置颜色值（ 样式：0xff00ff ）不带黑暗适配
public let HexRGB:((Int) -> UIColor) = { (rgbValue : Int) -> UIColor in
    return UIColor(red: CGFloat(CGFloat((rgbValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((rgbValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(rgbValue & 0xFF)/255), alpha: CGFloat(1.0))
}

//通过 十六进制与alpha设置颜色（样式：0xff00ff）带有黑暗适配
public let hexColorAlpha:((Int,Int,Float) -> UIColor) = {
    
    (lightValue:Int,darkValue:Int,alpha:Float) -> UIColor in
    
    if #available(iOS 13.0, *) {
        
        var byColor:UIColor = UIColor.init { (traitCollection) -> UIColor in
            
            if traitCollection.userInterfaceStyle == UIUserInterfaceStyle.light {
                
                return UIColor(red: CGFloat(CGFloat((lightValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((lightValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(lightValue & 0xFF)/255), alpha: CGFloat(alpha))
                
            }else{
                
                return UIColor(red: CGFloat(CGFloat((darkValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((darkValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(darkValue & 0xFF)/255), alpha: CGFloat(alpha))
                
            }
            
        }
        return byColor
        
    }else{
        
        return UIColor(red: CGFloat(CGFloat((lightValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((lightValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(lightValue & 0xFF)/255), alpha: CGFloat(alpha))
    }
}



//通过 十六进制设置颜色（样式：0xff00ff）带有黑暗适配
public let hexColor:((Int,Int) -> UIColor) = {
    
    (lightValue:Int,darkValue:Int) -> UIColor in
    
    if #available(iOS 13.0, *) {
        
        var byColor:UIColor = UIColor.init { (traitCollection) -> UIColor in
            
            if traitCollection.userInterfaceStyle == UIUserInterfaceStyle.light {
                
                return UIColor(red: CGFloat(CGFloat((lightValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((lightValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(lightValue & 0xFF)/255), alpha: CGFloat(1.0))
                
            }else{
                
                return UIColor(red: CGFloat(CGFloat((darkValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((darkValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(darkValue & 0xFF)/255), alpha: CGFloat(1.0))
                
            }
            
        }
        return byColor
        
    }else{
        
        return UIColor(red: CGFloat(CGFloat((lightValue & 0xFF0000) >> 16)/255), green: CGFloat(CGFloat((lightValue & 0xFF00) >> 8)/255), blue: CGFloat(CGFloat(lightValue & 0xFF)/255), alpha: CGFloat(1.0))
    }
}

// 通过 red 、 green 、blue 颜色数值
public let RGB:((Float,Float,Float) -> UIColor) = { (r: Float, g: Float , b: Float) -> UIColor in
    return UIColor.init(red: CGFloat(CGFloat(r)/255.0), green: CGFloat(CGFloat(g)/255.0), blue: CGFloat(CGFloat(b)/255.0), alpha: 1.0)
}

// 通过 red 、 green 、blue 、alpha 颜色数值
public let RGBAlpa:((Float,Float,Float,Float) -> UIColor) = { (r: Float, g: Float , b: Float , a: Float ) -> UIColor in
    return UIColor.init(red: CGFloat(CGFloat(r)/255.0), green: CGFloat(CGFloat(g)/255.0), blue: CGFloat(CGFloat(b)/255.0), alpha: CGFloat(a))
}

func hidePhoneNumber(number: String) -> String {
    
    //替换一段内容，两个参数：替换的范围和用来替换的内容
    let start = number.index(number.startIndex, offsetBy: 3)
    let end = number.index(number.startIndex, offsetBy: 8)
    let range = Range(uncheckedBounds: (lower: start, upper: end))
    return number.replacingCharacters(in: range, with: "*****")
    
}

// MARK: - 自定义颜色 ===================================================

// 颜色
public let cNavigationBarBgColor: NSString   = "#ffffff"
public let cStatusBarBgColor: NSString       = "#ffffff"
public let cTabBarBgColor: NSString          = "#494949"
public let cBackgroundColor: NSString        = "#F5F5F5"
public let cGapLineColor: NSString           = "#E1E3E6"
public let cDarkGrayColor: NSString          = "#989898"
public let cLightGrayColor: NSString         = "#d8d8d8"
public let cCoffeeColor: NSString            = "#895327"  // "#5F3916"  // "#59371C"   // "#8b572a"
public let cDarkCoffee: NSString             = "#5e3b1e"
public let cLightCoffee: NSString            = "#baa796"
public let cThemeYellow: NSString            = "#fdd649"
public let cLineColor: NSString              = "#eeeeee"
public let cTextColor: NSString              = "#333333"
public let cOrangeColor: NSString            = "#fb9648"
public let cLightPink: NSString              = "#fae4d7"


// MARK: - 自定义宏 ===================================================

public let kPageCount: NSInteger = 15

// MARK: - 自定义通知

public let nRootVCRefresh = NSNotification.Name(rawValue:"RootVCRefresh")
public let keepRecitWords = NSNotification.Name(rawValue:"KeepRecitWords") //继续背诵明天单词通知
public let loginSuc = NSNotification.Name(rawValue:"loginSuc")

// MARK: - 过滤 null ===================================================
///过滤null对象
public let kFilterNullOfObj:((Any)->Any?) = {(obj: Any) -> Any? in
    if obj is NSNull {
        return nil
    }
    return obj
}

///过滤null的字符串，当nil时返回一个初始化的空字符串
public let kFilterNullOfString:((Any)->String) = {(obj: Any) -> String in
    if obj is String {
        return obj as! String
    }
    return ""
}

/// 过滤null的数组，当nil时返回一个初始化的空数组
public let kFilterNullOfArray:((Any)->Array<Any>) = {(obj: Any) -> Array<Any> in
    if obj is Array<Any> {
        return obj as! Array<Any>
    }
    return Array()
}

/// 过滤null的字典，当为nil时返回一个初始化的字典
public let kFilterNullOfDictionary:((Any) -> Dictionary<AnyHashable, Any>) = {( obj: Any) -> Dictionary<AnyHashable, Any> in
    if obj is Dictionary<AnyHashable, Any> {
        return obj as! Dictionary<AnyHashable, Any>
    }
    return Dictionary()
}

// MARK: - 第三方配置 ===================================================
               
// MARK: - 通知 ===================================================
/** 支付*/
// method  0 余额  1 微信  2 支付宝   orderState  0成功 1 失败
let KNotificationPay  = "NotificationPay"
/** 订单*/
//orderState  0 取消订单  1 确认收货   2 删除订单
let KNotificationOrder  = "NotificationOrder"
/** 消息*/
//获取未读消息数量
let KNotificationUnreadMsg  = "NotificationUnreadMsg"



